﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;

public enum ProcessMonitoringType
{
    Run,
    NotRun,
    MinMemory,
    MaxMemory
}

public enum ProcessState
{
    OK,
    Fail
}

/// <summary>
/// Summary description for ProcessInfo
/// </summary>
public class ProcessInfo
{
    public ProcessInfo(string name, ProcessMonitoringType monitoringType)
    {
        ProcessName = name;
        MonitoringType = monitoringType;
        MemoryLimit = null;
    }
    
    public ProcessInfo(string name, ProcessMonitoringType monitoringType, long memoryLimit):this(name,monitoringType)
    {
        MemoryLimit = memoryLimit;
    }

    public string ProcessName {get;private set;}
    public long? MemoryLimit { get; private set; }

    public ProcessState State
    {
        get
        {
            switch (MonitoringType)
            {
                case ProcessMonitoringType.Run:
                    return ProcessRunning();                    
                case ProcessMonitoringType.NotRun:
                    return ProcessNotRunning();                    
                case ProcessMonitoringType.MinMemory:
                    return ProcessMinMemory();                    
                case ProcessMonitoringType.MaxMemory:
                    return ProcessMaxMemory();
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

    public int? Id
    {
        get
        {
            Process[] p = Process.GetProcessesByName(ProcessName);
            if (p.Length == 0)
                return null;
            return p[0].Id;
        }
    }

    public long? Memory
    {
        get
        {
            Process[] p = Process.GetProcessesByName(ProcessName);
            if (p.Length == 0)
                return null;
            return p[0].WorkingSet64;
        }
    }   

    public ProcessMonitoringType MonitoringType {get;private set;}    

    private ProcessState ProcessMaxMemory()
    {
        Process[] p = Process.GetProcessesByName(ProcessName);
        if (p.Length == 0)
            return ProcessState.Fail;

        if (p[0].WorkingSet64 > MemoryLimit)
            return ProcessState.Fail;

        return ProcessState.OK;
    }

    private ProcessState ProcessMinMemory()
    {
        Process[] p = Process.GetProcessesByName(ProcessName);
        if (p.Length == 0)
            return ProcessState.Fail;

        if (p[0].WorkingSet64 < MemoryLimit)
            return ProcessState.Fail;
        
        return ProcessState.OK;
    }

    private ProcessState ProcessRunning()
    {
        Process[] p = Process.GetProcessesByName(ProcessName);
        if (p.Length > 0)
            return ProcessState.OK;
        else
            return ProcessState.Fail;
    }

    private ProcessState ProcessNotRunning()
    {
        Process[] p = Process.GetProcessesByName(ProcessName);
        if (p.Length > 0)
            return ProcessState.Fail;
        else
            return ProcessState.OK;
    }
}
